package com.ricky.admin.util;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

public class NumUtil {

    public static String loadVerifyCode(){
        Random rand = new Random();
        int i = 0;
        String str = "";
        String s = null;
        int num = 5;
        while (i < num){
            switch (rand.nextInt(57)){

                case (0):

                case (47):

                case (24):
                    s = "0";
                    break;

                case (1):

                case (25):

                case (48):
                    s = "1";
                    break;

                case (2):

                case (26):

                case (49):
                    s = "2";
                    break;

                case (3):

                case (27):

                case (50):
                    s = "3";
                    break;

                case (4):
                    s = "4";
                    break;

                case (5):
                    s = "5";
                    break;

                case (6):
                    s = "6";
                    break;

                case (7):
                    s = "7";
                    break;

                case (8):
                    s = "8";
                    break;

                case (9):
                    s = "9";
                    break;

                case (10):
                    s = "A";
                    break;

                case (11):
                    s = "B";
                    break;

                case (12):
                    s = "C";
                    break;

                case (13):
                    s = "D";
                    break;

                case (14):
                    s = "E";
                    break;

                case (15):
                    s = "F";
                    break;

                case (16):
                    s = "G";
                    break;

                case (17):
                    s = "H";
                    break;

                case (18):
                    s = "I";
                    break;

                case (19):
                    s = "J";
                    break;

                case (20):
                    s = "K";
                    break;

                case (21):
                    s = "L";
                    break;

                case (23):
                    s = "M";
                    break;

                case (28):
                    s = "4";
                    break;

                case (29):
                    s = "5";
                    break;

                case (30):
                    s = "6";
                    break;

                case (31):
                    s = "7";
                    break;

                case (32):
                    s = "8";
                    break;

                case (33):
                    s = "9";
                    break;

                case (34):
                    s = "N";
                    break;

                case (35):
                    s = "O";
                    break;

                case (36):
                    s = "P";
                    break;

                case (37):
                    s = "Q";
                    break;

                case (38):
                    s = "R";
                    break;

                case (39):
                    s = "S";
                    break;

                case (40):
                    s = "T";
                    break;

                case (41):
                    s = "U";
                    break;

                case (42):
                    s = "V";
                    break;

                case (43):
                    s = "W";
                    break;

                case (44):
                    s = "X";
                    break;

                case (45):
                    s = "Y";
                    break;

                case (46):
                    s = "Z";
                    break;

                case (51):
                    s = "4";
                    break;

                case (52):
                    s = "5";
                    break;

                case (53):
                    s = "6";
                    break;

                case (54):
                    s = "7";
                    break;

                case (55):
                    s = "8";
                    break;

                case (56):
                    s = "9";
                    break;
                default:
                    s="";
            }
            i++;
            str = s + str;
            //防止根据随机字符串生成条形码图片时报错
            if(str.contains("NULL")||str.contains("null")){
                i=0;
            }
        }
        return str;
    }

    public static void main(String[] args) {

        System.out.println(NumUtil.loadVerifyCode());
    }

    public static String toConver_(String str){
        String[] strArr = new String[]{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
        for(int i=0;i<strArr.length;i++){
            if(str.indexOf(strArr[i])>0){
                str = str.replace(strArr[i], "_"+strArr[i]);
            }
        }
        return str;
    }

    private static final AtomicInteger sequence = new AtomicInteger(0);
    private static LocalDate lastDate = LocalDate.now();

    private static final AtomicInteger counter = new AtomicInteger(0);


    public static synchronized String generateUniqueId() {
        LocalDate currentDate = LocalDate.now();
        if (!currentDate.equals(lastDate)) {
            // 如果日期改变了，重置序列号
            sequence.set(0);
            lastDate = currentDate;
        }
        // 获取当前日期的年月日部分
        String datePart = currentDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        long dateValue = Long.parseLong(datePart);
        // 生成5位数的ID，使用getAndIncrement()方法确保线程安全
        int id = counter.getAndIncrement() % 100000; // 100000代表5位数的最大值
        // 如果ID小于10000（4位数），则前面补0，以确保总是返回5位数的字符串
        //long idValue = id < 10000 ? id + 10000 : id;
        String s = loadVerifyCode();
        return dateValue + s;
    }
}
